# -*- coding: utf-8 -*-
import sys
import json
import threading
from ws.webscoket_server import WebsocketServer

# Reload does the trick!
reload(sys)
sys.setdefaultencoding('UTF-8')



# Called for every client connecting (after handshake)
def client_open(client, server):
    print 'client open connected, id:', client['protocol']
    # server.send_message_to_all("Hey all, a new client has joined us")


# Called for every client disconnecting
def client_close(client, server):
    print 'client close connected, id:', client['id']


# Called when a client sends a message
def message_received(client, server, message):
    print 'recv client id:', client['id'], 'message:', message


if __name__ == "__main__":
    server = WebsocketServer(8000, host='127.0.0.1')

    server.set_fn_client_open(client_open)
    server.set_fn_client_close(client_close)
    server.set_fn_message_received(message_received)
    server.run_forever()

